<?php
    /**
    * Object.php
    * 
    * @author Osuvaldo Ramos <xdracox@gmail.com>
    * @copyright Copyright (c) 2008, Osuvaldo Ramos
    * @license http://www.opensource.org/licenses/bsd-license.php
    * @package Atman
    */
    
    require_once 'Log.php';
    
    /**
    * The base class for all the classes in the framework. It provides basic
    * logging utilities.
    * @package Atman
    */
    abstract class AT_Object
    {
        /**
        * Contains the Log instance the instance of this class will use to log.
        * @var Log
        */
        protected $log;
        
        /**
        * Keeps an instance of ReflectionClass of this object.
        * @var ReflectionClass
        */
        protected $me;
        
        /**
        * The constructor defines the logger and creates the instance of the
        * ReflectionClass stored in $me.
        */
        public function __construct()
        {
            $logFile = AT_ROOT.'/logs/'.date('Y-m-d').'.log';
            $this->me = new ReflectionClass($this);
            
            $className = str_replace('_', '::', $this->me->getName());
            $conf = array('mode' => 0777, 'dirmode' => 0777);
            $this->log = Log::singleton('file', $logFile, $className, $conf);
        }
        
        /**
        * Logs an error and takes appropiate action depending on the level
        * of severity.
        * @param string $message The error message.
        * @param int $level The level of severity of the error. The level should
        *                   be one of the log levels defined the in the PEAR
        *                   Log package.
        * @return void
        * @todo Do something in case of an unrecoverable error.
        */
        protected function reportError($message, $level)
        {
            $this->log->log($message, $level);
            $fatal = PEAR_LOG_EMERG | PEAR_LOG_ALERT | PEAR_LOG_CRIT
                | PEAR_LOG_ERR | PEAR_LOG_WARNING;
            if ( $fatal & $level != 0 )
            {
                // if a severe enough error occurs, this method acts as a mini
                // controller using the smarty view to handle the situation
                $type = ($level == PEAR_LOG_WARNING) ? 'warning' : 'error';
                
                $controller = new AT_Controller('error', 'errorhandler', $type);
                $controller->start($message);
                
                exit();
            }
        }
        
        /**
        * Sets the object's properties from an array.
        * @param array $from The variable data.
        * @return void
        */
        public function setFrom($from)
        {
            $props = $this->me->getProperties();
            foreach ( $props as $prop )
            {
                $var = $prop->getName();
                if ( isset($from[$var]) && !$prop->isPrivate() )
                {
                    $this->$var = $from[$var];
                }
            }
        }
        
        /**
        * The destructor closes the logger.
        */
        public function __destruct()
        {
            $this->log->close();
        }
    }
?>
